
var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
var mph = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hide_Popup_blen: false,
    is_default: 0,
    spay_default: 0,
    sele_vipDataList: [],
    pay_list: [
      // { type: 'wechat', name: '微信支付', title: '亿万用户的选择，更快更安全', img: '/image/icon64_appwx_logo.png', is_default: false },
      { type: 'balance', name: '余额支付', title: '余额支付', img: '/image/icon64_appwx_logo.png', is_default: true },
    ],
    pay_select:'',
    vip_select:'',
    user_info:{},
    resd:'',
    user_asset:{},
    showPayPwdInput: false,  //是否展示密码输入层
    pwdVal: '',  //输入的密码
    payFocus: true, //文本框焦点
  },
  my_info() {
    let that = this;
    //获得我的信息
    var url = 'index.php?act=member_index';
    mph.postRequest(url, {}, function (res) {
      
      if (res.data.code == 200) {
        let resd = (res.data.datas.member_info.level_info.exppoints_rate / res.data.datas.member_info.level_info.upgrade_exppoints * 100) + '%';
        that.setData({
          user_info: res.data.datas.member_info,
          resd: resd
        })
      } else {
        mph.showErrorTost(res.msg);
      }
    })


    //获取用户的资金情况
    var url = 'index.php?act=member_index&op=my_asset';
    mph.postRequest(url, {}, function (res) {
      
      if (res.data.code == 200) {

        // if (res.data.datas.predepoit >= 10000) {
        //   res.data.datas.predepoit = '¥' + parseInt(parseInt(res.data.datas.predepoit) / 10000) + '万';
        // }
        // if (res.data.datas.point >= 10000) {
        //   res.data.datas.point = parseInt(parseInt(res.data.datas.point) / 10000) + '万';
        // }
        // if (res.data.datas.voucher >= 10000) {
        //   res.data.datas.voucher = parseInt(parseInt(res.data.datas.voucher) / 10000) + '万';
        // }

        that.setData({
          user_asset: res.data.datas
        })
        // wx.hideToast();
      } else {
        mph.showErrorTost(res.msg);
      }
    })
  },
  //卡类型选择
  radioChange: function (e) {
    // console.log(e);
    let ind = e.detail.value;
    let that = this;
    that.setData({
      is_default: ind,
      vip_select: that.data.sele_vipDataList[ind]
    });
  },

//支付选择器
  radioPay:function (e){
    let value = e.detail.value;
    let that = this;
    this.setData({
      spay_default:value,
      pay_select: that.data.pay_list[value]
    })
  },

  show_Popup_box(){
    let that = this;
    that.data.hide_Popup_blen = true;
    that.setData({
      hide_Popup_blen: that.data.hide_Popup_blen
    })
  },
  hide_Popup_box() {
    let that = this;
    that.data.hide_Popup_blen = false;
    that.setData({
      hide_Popup_blen: that.data.hide_Popup_blen
    })
  },
  pay:function(){
    this.showInputLayer();
  },
  //支付
  pay_pay:function (){
    
    let that = this;
    var vip = that.data.vip_select;
    var pay = that.data.pay_select;
    var type_id = vip.id
    var money = vip.money
    var pay_type = pay.type
    var url = 'index.php?act=vipopen&op=vip_pay';
    let predepoit = parseFloat(that.data.user_asset.predepoit)
    
    if (predepoit > 0 && predepoit >= money) {
      if (type_id || money || pay_type) {
        mph.postRequest(url, { type_id: type_id, money: money, pay_type: pay_type }, function (res) {
          if (res.data.code == 200) {
            wx.showToast({
              title: res.data.msg,
              icon: 'success',
              duration: 3000
            });
            wx.switchTab({
              url: '../../pages/personal_center/personal_center'
            })

          } else {
            mph.showErrorTost();
          }
        });
      }
    }else{
      // mph.showErrorTost('您的余额不足，请充值');
      wx.navigateTo({
        url: '/pages/my_balance/my_balance'
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(ApiUrl)
    this.my_info(); 
    var that =this;
    mph.is_yue_pass()
    var list =  mph.postRequest("index.php?act=vipopen&op=list",{},function(res){
      console.log('pay_select1111111', that.data.pay_list);
      if (res.data.code == 200) {
        that.setData({
          sele_vipDataList:res.data.datas,
          pay_select: that.data.pay_list[that.data.spay_default],
          vip_select: res.data.datas[that.data.is_default]
        })
      }
   });
  },/**
   * 显示支付密码输入层
   */
  showInputLayer: function () {
    mph.is_yue_pass();
    this.setData({ showPayPwdInput: true, payFocus: true });
  },
  /**
   * 隐藏支付密码输入层
   */
  hidePayLayer: function () {

    var val = this.data.pwdVal;

    this.setData({ showPayPwdInput: false, payFocus: false, pwdVal: '' });

  },
  /**
   * 获取焦点
   */
  getFocus: function () {
    this.setData({ payFocus: true });
  },
  /**
   * 输入密码监听
   */
  inputPwd: function (e) {
    var that =  this;
    that.setData({ pwdVal: e.detail.value });

    if (e.detail.value.length >= 6) {
      //执行密码验证
      var url = 'index.php?act=member_buy&op=check_password';
      mph.postRequest(url, { password: e.detail.value}, function (res) {
        if(res.data.code==200){
          that.hidePayLayer();
          that.pay_pay()//执行支付过程
        }else{
          mph.showErrorTost();
        }
      })
      
    }
  }
})